package com.orion.app.domain.entity;

import javax.persistence.EntityManager;
import javax.persistence.ManyToOne;
import javax.persistence.Transient;
import javax.persistence.TypedQuery;
import javax.validation.constraints.NotNull;
import org.springframework.roo.addon.javabean.RooJavaBean;
import org.springframework.roo.addon.jpa.activerecord.RooJpaActiveRecord;
import org.springframework.roo.addon.tostring.RooToString;

@RooJavaBean
@RooToString
@RooJpaActiveRecord(finders = { "findUserRolesByUserEntry" })
public class UserRole {

    @NotNull
    @ManyToOne
    private User userEntry;

//    @NotNull
    @ManyToOne
    private Role roleEntry;

    @Transient
    private String roleNames;

    public String getRoleNames() {
        return roleNames;
    }

    public void setRoleNames(String roleNames) {
        this.roleNames = roleNames;
    }

    public static TypedQuery<UserRole> findUserRolesByUserEntry(User userEntry) {
        if (userEntry == null) throw new IllegalArgumentException("The userEntry argument is required");
        EntityManager em = UserRole.entityManager();
        TypedQuery<UserRole> q = em.createQuery("SELECT o FROM UserRole AS o WHERE o.userEntry = :userEntry", UserRole.class);
        q.setParameter("userEntry", userEntry);
        return q;
    }
}
